package com.donleo.student.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.donleo.common.api.CommonResult;
import com.donleo.common.model.Sign;
import com.donleo.common.model.User;
import com.donleo.common.utils.DateUtil;
import com.donleo.student.mapper.SignMapper;
import com.donleo.student.service.SignService;
import com.donleo.student.vo.SignVo;
import com.donleo.student.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 签到表 服务实现类
 * </p>
 *
 * @author liangd
 * @since 2020-12-21
 */
@Service
public class SignServiceImpl extends ServiceImpl<SignMapper, Sign> implements SignService {

    @Autowired
    private SignMapper signMapper;

    @Override
    public List<User> findAllStudent(UserVo userVo) {
        return signMapper.findAllStudent(userVo);
    }

    @Override
    public long getCountStudent(UserVo userVo) {
        return signMapper.getCountStudent(userVo);
    }

    @Override
    public List<User> findTeamMemberByUserCode(String userCode) {
        //当天时间 yyyy-MM-dd 00:00:00
        String currentDay = DateUtil.getCurrentDay();
        //下一天时间 yyyy-MM-dd 00:00:00
        String nextDay = DateUtil.getNextDay();
        List<User> list = new ArrayList<>();
        //查询当天已经签到的组员
        List<User> signedList = signMapper.findSignedStudent(userCode, currentDay, nextDay);
        //查询该组的组员
        List<User> stuList = signMapper.findTeamMemberByUserCode(userCode);
        for (User aStuList : stuList) {
            User user = new User();
            user.setId(aStuList.getId());
            user.setCode(aStuList.getCode());
            user.setUsername(aStuList.getUsername());
            user.setAvatar(aStuList.getAvatar());
            user.setNickname(aStuList.getNickname());
            user.setPhone(aStuList.getPhone());
            user.setTeamName(aStuList.getTeamName());
            list.add(user);
            //表示有已经签到的学生
            if (signedList.size() > 0) {
                for (User aSignedList : signedList) {
                    User model = new User();
                    model.setId(aSignedList.getId());
                    //判断学生是否已经签到，已经签到的学生就移除
                    if (model.getId().equals(user.getId())) {
                        list.remove(user);
                    }
                }
            }
        }

        return list;
    }

    @Override
    public CommonResult saveSignData(List<Sign> list) {
        List<Sign> lists = new ArrayList<>();
        for (Sign aList : list) {
            Sign sign = new Sign();
            sign.setUserCode(aList.getUserCode());
            sign.setSignTime(aList.getSignTime());
            //判断迟到时间是否为空
            if (aList.getLateTime() != null && !"".equals(aList.getLateTime())) {
                sign.setLateTime(DateUtil.getDate(aList.getLateTime()));
            }
            sign.setSignStatus(aList.getSignStatus());
            lists.add(sign);
        }
        int i = signMapper.saveSignData(lists);
        if (i > 0) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @Override
    public List<UserVo> findAllSignRecord(UserVo userVo) {
        return signMapper.findAllSignRecord(userVo);
    }

    @Override
    public long getCountSignRecord(UserVo userVo) {
        return signMapper.getCountSignRecord(userVo);
    }

    @Override
    public CommonResult delete(Integer id) {
        int i = signMapper.deleteById(id);
        if (i > 0) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult deletes(List<Integer> idsList) {
        int i = signMapper.deleteBatchIds(idsList);
        if (i > 0) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult updateSign(Sign sign) {
        sign.setLateTime(DateUtil.getDate(sign.getLateTime()));
        //如果修改状态不为迟到，则设置迟到时间为空
        if (!"late".equals(sign.getSignStatus())) {
            sign.setLateTime("");
        }
        int i = signMapper.updateById(sign);
        if (i > 0) {
            return CommonResult.success(i);
        }
        return CommonResult.failed();
    }

    @Override
    public SignVo findById(Integer id) {
        return signMapper.findById(id);
    }
}
